package com.bg.print.config.securityconfig;

import com.alibaba.fastjson2.JSON;
import com.bg.print.utils.R;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import java.io.IOException;

/**
 * @program: bg-print-java
 * @description: 认证成功相应结果
 * @author: liuxvru
 * @create: 2024-04-03 10:35
 **/
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

        //获取用户身份信息
        Object principal = authentication.getPrincipal();
        //转换成json字符串
        String json = JSON.toJSONString(R.ok("登录成功").put("data",principal));

        //返回响应
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(json);
    }
}
